package com.example.whac_a_mole;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Activity_login extends AppCompatActivity {
   private Button button_login;
   private Button button_submit;
   private Button button_reinput;
   private Button button_setpass;
   private EditText edit_num;
   private EditText edit_passwd;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        button_login=findViewById(R.id.button_login);
        button_submit=findViewById(R.id.button_submit);
        button_reinput=findViewById(R.id.button_reinput);
        button_setpass=findViewById(R.id.button_setpass);
        edit_num=findViewById(R.id.edit_num);
        edit_passwd=findViewById(R.id.edit_passwd);
        button_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String user=edit_num.getText().toString().trim();
                String passwd=edit_passwd.getText().toString().trim();
                if(!user.equals("")&&!passwd.equals(""))
                {
                    int count = new DataBase(Activity_login.this).login(user,passwd);
                    if(count!=0)
                    {
                        Intent intent=new Intent(Activity_login.this, Activity_selected.class);
                        intent.putExtra("user",user);
                        startActivity(intent);
                        Toast.makeText(Activity_login.this,"登录成功！欢迎"+user+"!",Toast.LENGTH_SHORT).show();
                    }
                    else
                    {
                        //清空输入框内容
                        edit_num.setText("");
                        edit_passwd.setText("");
                        Toast.makeText(Activity_login.this,"登录失败，请检测账号和密码",Toast.LENGTH_SHORT).show();
                    }
                }
                else
                {
                    Toast.makeText(Activity_login.this,"密码和账号不能为空！",Toast.LENGTH_SHORT).show();
                }

            }
        });
        button_reinput.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                edit_num.setText("");
                edit_passwd.setText("");
            }
        });
        button_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(Activity_login.this,Activity_submit.class));
            }
        });
        button_setpass.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(Activity_login.this,Activity_find.class));
            }
        });



    }
}
